#include "Graphics.h"
#include <SDL.h>
#include "Logger.h"
#include <string>
#include "Helpers.h"
#include <Windows.h>
#include <SDL_image.h>

using namespace std;

Graphics::Graphics() {
	
	this->windowSize.x = GetSystemMetrics(SM_CXSCREEN);
	this->windowSize.y = GetSystemMetrics(SM_CYSCREEN);

	if(SDL_Init(SDL_INIT_EVERYTHING) != 0) {
		string error = "SDL_Init failed: ";
		error += SDL_GetError();
		Logger::Log(0, error, __FILE__, __LINE__, LOG_ERROR);
		Helpers::PostQuitMessage();
	}

	this->window = SDL_CreateWindow("Prototype01", 0, 0, this->windowSize.x, this->windowSize.y, SDL_WINDOW_FULLSCREEN);

	if(this->window == NULL) {
		string error = "SDL_CreateWindow Error: ";
		error += SDL_GetError();
		Logger::Log(0, error, __FILE__, __LINE__, LOG_ERROR);
		Helpers::PostQuitMessage();
	}

	IMG_Init(IMG_INIT_PNG);

	this->renderer = SDL_CreateRenderer(this->window, -1, SDL_RENDERER_ACCELERATED);
	
}

void Graphics::RenderFrame() {

	SDL_SetRenderDrawColor(this->renderer, 0, 0, 100, 255);
	SDL_RenderClear(this->renderer);

	SDL_RenderPresent(this->renderer);

}

SDL_Texture* Graphics::LoadTexture(const std::string &file, SDL_Renderer *ren) {

	SDL_Texture* texture = NULL;
	SDL_Surface* loadedImage = IMG_Load(file.c_str());

	if(loadedImage != NULL) {
		texture = SDL_CreateTextureFromSurface(this->renderer, loadedImage);
		SDL_FreeSurface(loadedImage);
		
		if(texture == NULL) {
			string error = "Failed to load Texture: ";
			error += SDL_GetError();
			Logger::Log(0, error, __FILE__, __LINE__, LOG_ERROR);
		}

	}
	else {
		string error = "Failed to load Texture: ";
		error += IMG_GetError();
		Logger::Log(0, error, __FILE__, __LINE__, LOG_ERROR);
	}

	return texture;
}